﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;

namespace BBMS.DLL.Forms.Catalog.BillType
{
    public partial class frmBillTypeUpdate : Base.FrmEditBase
    {
        public frmBillTypeUpdate(BBMS.DLL.Base.ObjectBase obj)
        {
            InitializeComponent();
            curObj = obj;
        }

        public new static readonly string FunctionKey = "DMLoaiChungTu";
        protected override string GetFunctionKey() { return "DMLoaiChungTu"; }

        protected override void OnLoad()
        {
            if (curObj != null)
            {
                txtMa.Text = ((LoaiChungTu)curObj).Ma;
                txtTen.Text = ((LoaiChungTu)curObj).Ten;
                bool isChi = ((LoaiChungTu)curObj).IsChi;
                bool hasVAT = ((LoaiChungTu)curObj).HasVat;
                txtTen.Focus();
                txtMa.ReadOnly = true;

                if(!Config.CurUser.HaveRole(Role.Edit,GetFunctionKey())){
                    txtTen.ReadOnly = true;
                    chkHasVAT.IsAccessible = false;
                    chkIsChi.IsAccessible = false;
                    btnSaveNew.Enabled =false;
                    btnReset.Enabled = false;
                    btnSaveClose.Enabled = false;
                }
            }
            else
            {
                txtMa.Focus();
            }
        }

        protected override ObjectBase GetEditedObject() {
            if (txtMa.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập mã.");
                txtMa.Focus();
                return null;
            }
            if (txtTen.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập tên.");
                txtTen.Focus();
                return null;
            }


            return new LoaiChungTu(txtMa.Text.Trim().ToUpper(),
                                txtTen.Text.Trim(),
                               chkIsChi.Checked, chkHasVAT.Checked); 
        }

        protected override void Reset()
        {
            txtMa.ReadOnly = false;
            txtMa.Text = "";
            txtTen.Text = "";
            chkIsChi.Checked = false;
            chkHasVAT.Checked = false;
            txtMa.Focus();

            if (curObj != null) OnLoad();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnClose();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        
        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            OnSaveAndNew();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            OnSaveAndClose();
        }
    }
}
